import { ref } from 'vue'

export interface ProjectConfiguration {
  id: number
  key: string
  value: string
  comment: string
}

export function useGetProjectConfigurations(baseUrl: string, params: { id: number }) {
  const configurations = ref<ProjectConfiguration[]>([])
  const loading = ref(false)
  const error = ref<string | null>(null)

  async function fetchConfigurations() {
    loading.value = true
    error.value = null
    try {
      const url = `${baseUrl}/api/project/configurations?id=${params.id}`
      const res = await fetch(url)
      if (!res.ok) throw new Error('网络错误')
      configurations.value = await res.json()
    } catch (e: any) {
      error.value = e.message || '未知错误'
    } finally {
      loading.value = false
    }
  }

  return { configurations, loading, error, fetchConfigurations }
}
